package com.icinfo.cloud.provider.punish.strategy.document;

import com.icinfo.cloud.provider.punish.document.DocumentFieldCustomConverter;
import com.icinfo.cloud.provider.punish.document.annotations.DocumentParamConvert;
import com.icinfo.cloud.provider.punish.document.dto.PrintDocumentBuildDto;
import com.icinfo.cloud.provider.punish.print.vo.ImageContentVo;
import com.icinfo.cloud.provider.punish.print.vo.PrintContentVo;
import com.icinfo.cloud.provider.punish.print.vo.PrintDocumentVo;
import com.icinfo.cloud.provider.punish.print.vo.TitleOrContentVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 责令改正通知书文书参数
 *
 * @author Junfeng.Chen
 * @date 2024/1/15 8:49
 **/
@Data
@Slf4j
public class ZlgztzsDocumentParam implements BaseDocumentParam {
    /**
     * 单位名称
     */
    @DocumentParamConvert(narrow = "orgStandardName", standard = "orgStandardName")
    private String orgStandardName;

    /**
     * 文书号
     */
    @DocumentParamConvert(narrow = "documentNumber", standard = "documentNumber")
    private String documentNumber;

    /**
     * 当事人名称
     */
    @DocumentParamConvert(narrow = "partyName", standard = "partiesName")
    private String partyName;

    /**
     * 当事人地址
     */
    @DocumentParamConvert(narrow = "autoFill_address", standard = "partiesAddress")
    private String partyAddress;

    /**
     * 案发时间，宽幅不包含该字段的直接映射
     */
    @DocumentParamConvert(narrow = "ZGT_AFRQ")
    private String caseHappenTime;

    /**
     * 案发地址，宽幅不包含该字段的直接映射
     */
    @DocumentParamConvert(narrow = "addressWithLatAndLgt")
    private String addressWithLatAndLgt;

    /**
     * 违法行为，宽幅不包含该字段的直接映射
     */
    @DocumentParamConvert(narrow = "ZGT_WFXW")
    private String wfxw;

    /**
     * 具体违法内容，窄幅不需要该变量，宽幅需要根据窄幅参数进行转换
     */
    @DocumentParamConvert(standard = "ZLGZTZS_AFJG")
    private DocumentFieldCustomConverter illegalContentDetail = params -> {
        //${case_main_detail_info.case_register_time}，在${case_main_detail_info.case_happen_address_detail}实施${case_main_detail_info.case_reason}的违法行为
        //案发时间，
        String caseHappenTime = String.valueOf(Optional.ofNullable(params.get("ZGT_AFRQ")).orElse(""));
        // 案发地址
        String addressWithLatAndLgt = String.valueOf(Optional.ofNullable(params.get("addressWithLatAndLgt")).orElse(""));
        //  违法行为
        String wfxw = String.valueOf(Optional.ofNullable(params.get("ZGT_WFXW")).orElse(""));
        return caseHappenTime + "在" + getNarrowCaseAddress(addressWithLatAndLgt) + "实施" + wfxw + "的违法行为";
    };

    /**
     * 违则，宽幅不包含该字段的直接映射
     */
    @DocumentParamConvert(narrow = "ZGT_WZ", standard = "ZLGZTZS_WZ")
    private String wz;

    /**
     * 依据，罚则
     */
    @DocumentParamConvert(narrow = "ZGT_FZ", standard = "ZLGZTZS_YJ")
    private String yj;

    /**
     * 责令明细
     */
    @DocumentParamConvert(narrow = "ZGT_ZLMX", standard = "ZLGZTZS_GZYQ")
    private String zlmx;

    /**
     * 复议政府
     */
    @DocumentParamConvert(narrow = "ZGT_FYZF", standard = "ZGT_FYZF")
    private String fyzf;

    /**
     * 复议法院
     */
    @DocumentParamConvert(narrow = "ZGT_FYFY", standard = "ZGT_FYFY")
    private String fyfy;

    /**
     * 当事人签名
     */
    @DocumentParamConvert(narrow = "signFingerprintPic", standard = "signFingerprintPic")
    private String partySign;

    /**
     * 联系人
     */
    @DocumentParamConvert(narrow = "ZGT_LXR", standard = "ZGT_LXR")
    private String linkman;

    /**
     * 联系人电话
     */
    @DocumentParamConvert(narrow = "ZGT_PHONE", standard = "ZGT_PHONE")
    private String linkPhone;

    /**
     * 联系地址
     */
    @DocumentParamConvert(narrow = "ZGT_ADDR", standard = "ZGT_ADDR")
    private String linkAddress;

    /**
     * 不签字原因，为null代表没有不签字的逻辑，否则为有不签字逻辑
     */
    @DocumentParamConvert(narrow = "notSignReason", standard = "notSignReason")
    private String notSignReason;

    /**
     * 签章单位名称
     */
    @DocumentParamConvert(narrow = "orgStandardNameSeal", standard = "orgStandardNameSealLocation")
    private String orgStandardNameSeal;

    /**
     * 签章时间
     */
    @DocumentParamConvert(narrow = "sealDate", standard = "sealDate")
    private String sealDate;

    /**
     * json打印时使用的签章图片url
     */
    @DocumentParamConvert(narrow = "sealPic", standard = "sealPic")
    private String sealPic;


    @Override
    public PrintDocumentVo assemblyPrintContent(PrintDocumentBuildDto dto) {
        //根据窄幅参数构建打印信息
        PrintContentVo vo = new PrintContentVo();
        vo.setTitle(assemblyTitle());
        vo.setPartyInfo(assemblyPartyInfo());
        vo.setBodyContent(assemblyBodyContent());
        vo.setImageInfo(assemblyImageInfo(dto));
        return new PrintDocumentVo(null, vo);
    }

    /**
     * 组装图片信息内容
     *
     * @param dto 签章参数
     * @return
     */
    private List<ImageContentVo> assemblyImageInfo(PrintDocumentBuildDto dto) {
        List<ImageContentVo> imageInfo = new LinkedList<>();
        //当事人签字
        imageInfo.add(new ImageContentVo()
                .setDescription("当事人：")
                //url转base64存入
                .setImageBase64(Collections.singletonList(convertImageUrlToBase64(partySign)))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        //执法人员信息
        imageInfo.add(new ImageContentVo()
                .setDescription("联系人：" + Optional.ofNullable(linkman).orElse(""))
                .setImageBase64(Collections.singletonList(""))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        imageInfo.add(new ImageContentVo()
                .setDescription("联系电话：" + Optional.ofNullable(linkPhone).orElse(""))
                .setImageBase64(Collections.singletonList(""))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        imageInfo.add(new ImageContentVo()
                .setDescription("联系地址：" + Optional.ofNullable(linkAddress).orElse(""))
                .setImageBase64(Collections.singletonList(""))
                .setImagePosition(1).setComment(Collections.emptyList()).initNormalStyle());
        //获取签章图片
        addDeptSealPic(imageInfo, sealPic, dto);
        //当事人未签字原因
        addNotSignReason(imageInfo, notSignReason);
        return imageInfo;
    }

    private List<TitleOrContentVo> assemblyTitle() {
        List<TitleOrContentVo> title = new LinkedList<>();
        title.add(new TitleOrContentVo(Optional.ofNullable(orgStandardName).orElse("")).initTitleStyle());
        title.add(new TitleOrContentVo("责令改正通知书").initTitleStyle());
        title.add(new TitleOrContentVo(Optional.ofNullable(documentNumber).orElse("")).initDocumentNumberStyle());
        return title;
    }

    /**
     * 构建当事人信息
     *
     * @return
     */
    private List<TitleOrContentVo> assemblyPartyInfo() {
        List<TitleOrContentVo> partyInfo = new LinkedList<>();
        partyInfo.add(new TitleOrContentVo("当事人：" + Optional.ofNullable(partyName).orElse("")).initNormalStyle());
        partyInfo.add(new TitleOrContentVo()
                .setContent("地址：" + Optional.ofNullable(partyAddress).orElse("")).initNormalStyle());
        return partyInfo;
    }

    /**
     * 构建内容
     *
     * @return
     */
    private List<TitleOrContentVo> assemblyBodyContent() {
        //构建内容
        List<TitleOrContentVo> bodyContent = new LinkedList<>();
        //第一段
        String content1 = "    经查，你（单位）于" +
                Optional.ofNullable(caseHappenTime).orElse("") +
                "在" + getNarrowCaseAddress(addressWithLatAndLgt) +
                Optional.ofNullable(wfxw).orElse("") +
                "。你（单位）的上述行为已经违反了" +
                Optional.ofNullable(wz).orElse("") +
                "的规定。";
        //第二段
        String content2 = "    依据" + Optional.ofNullable(yj).orElse("") +
                "的规定，现责令你（单位）" + Optional.ofNullable(zlmx).orElse("") + "。";
        //第三段
        String content3 = "    如不服本通知，可以在收到本通知书之日起60日内向" + Optional.ofNullable(fyzf).orElse("") +
                "申请行政复议；也可以在收到本通知书之日起6个月内直接向" + Optional.ofNullable(fyfy).orElse("") + "起诉。";
        bodyContent.add(new TitleOrContentVo(content1).initNormalStyle());
        bodyContent.add(new TitleOrContentVo(content2).initNormalStyle());
        bodyContent.add(new TitleOrContentVo(content3).initNormalStyle());
        return bodyContent;
    }
}
